package uk.ac.ebi.bioinvindex.model.impl.term;

import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Store;
import uk.ac.ebi.bioinvindex.model.impl.AnnotatableImpl;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

/**
 * User: Nataliya Sklyar (nsklyar@ebi.ac.uk)
 * Date: Jul 12, 2007
 */

@MappedSuperclass
public abstract class FreeTextTermImpl extends AnnotatableImpl{

	@Field(index = Index.TOKENIZED,store = Store.YES )
	protected String value;

	protected Collection<OntologyTermImpl> ontologyTerms = new ArrayList<OntologyTermImpl>();

	protected FreeTextTermImpl() {
	}

	protected FreeTextTermImpl( String acc, String value) {
		this.setAcc ( acc );
		this.setValue(value);
	}


	protected FreeTextTermImpl(String value) {
		this.value = value;
	}



	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	@Transient
	public Collection<OntologyTermImpl> getOntologyTerms() {
		return Collections.unmodifiableCollection(ontologyTerms);
	}

	protected void setOntologyTerms(Collection<OntologyTermImpl> ontologyTerms) {
		this.ontologyTerms = ontologyTerms;
	}

	public void addOntologyTerm(OntologyTermImpl term) {
		this.ontologyTerms.add(term);
	}

	public boolean removeOntologyTerm(OntologyTermImpl term) {
		return ontologyTerms.remove(term);
	}

	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		if (!super.equals(o)) return false;

		FreeTextTermImpl term = (FreeTextTermImpl) o;

		if (value != null ? !value.equals(term.value) : term.value != null) return false;
		if (ontologyTerms != null ? !ontologyTerms.equals(term.ontologyTerms) : term.ontologyTerms != null) return false;

		return true;
	}

	public int hashCode() {
		int result = super.hashCode();
		result = 31 * result + (value != null ? value.hashCode() : 0);
		result = 31 * result + (ontologyTerms != null ? ontologyTerms.hashCode() : 0);
		return result;
	}


	public String toString() {
		return "FreeTextTermImpl{" +
				"name='" + value + '\'' +
				", ontologyTerms=" + ontologyTerms +
				'}';
	}
}
